package com.huika.dealontop.anim.berzier;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.PointF;
import android.view.View;
import android.view.ViewGroup;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2016/1/8 14:25 <br/>
 * Description: 基于Bezier曲线的移动动画
 */
public class BezierTravelAnim {
    private PointF startP, endP;
    private View animView;
    private ViewGroup animContainer;
    private Activity context;
    private int duration = 1500; //默认1500毫秒

    private Animator.AnimatorListener animatorListener;

    /**
     * 私有无参构造方法，仅提供给{@link Builder}使用
     */
    private BezierTravelAnim() {
    }

    /**
     * 构造方法参数太多，建议使用 {@link Builder} 构造器
     *
     * @param context
     * @param animView
     * @param startP
     * @param endP
     */
    @Deprecated
    public BezierTravelAnim(Activity context, View animView, PointF startP, PointF endP) {
        this.context = context;
        this.animContainer = (ViewGroup) context.getWindow().getDecorView();
        this.animView = animView;
        this.startP = startP;
        this.endP = endP;
    }

    public void startAnimation() {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new BezierEvaluator(), startP, endP);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                animView.setX(pointF.x);
                animView.setY(pointF.y);
            }
        });

        valueAnimator.addListener(new Animator.AnimatorListener() {
            public void onAnimationStart(Animator animation) {
                animContainer.addView(animView);
                if(animatorListener != null){
                    animatorListener.onAnimationStart(animation);
                }
            }

            public void onAnimationEnd(Animator animation) {
                animContainer.removeView(animView);
                animView.destroyDrawingCache();
                if(animatorListener != null){
                    animatorListener.onAnimationEnd(animation);
                }
            }

            public void onAnimationCancel(Animator animation) {
                if(animatorListener != null){
                    animatorListener.onAnimationCancel(animation);
                }
            }

            public void onAnimationRepeat(Animator animation) {
                if(animatorListener != null){
                    animatorListener.onAnimationRepeat(animation);
                }
            }
        });

        AnimatorSet animatorSet = new AnimatorSet();
        animView.setPivotX(0);
        animView.setPivotY(0);
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(animView, "scaleX", 1f, 0.04f),
                ObjectAnimator.ofFloat(animView, "scaleY", 1f, 0.04f),
                valueAnimator
        );
        animatorSet.setDuration(duration);
        animatorSet.start();
    }

    public PointF getStartP() {
        return startP;
    }

    public void setStartP(PointF startP) {
        this.startP = startP;
    }

    public PointF getEndP() {
        return endP;
    }

    public void setEndP(PointF endP) {
        this.endP = endP;
    }

    public View getAnimView() {
        return animView;
    }

    public void setAnimView(View animView) {
        this.animView = animView;
    }

    public Activity getContext() {
        return context;
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public ViewGroup getAnimContainer() {
        return animContainer;
    }

    public void setAnimContainer(ViewGroup animContainer) {
        this.animContainer = animContainer;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public Animator.AnimatorListener getAnimatorListener() {
        return animatorListener;
    }

    public void setAnimatorListener(Animator.AnimatorListener animatorListener) {
        this.animatorListener = animatorListener;
    }

    /**
     * 构造器
     */
    public static class Builder {
        BezierTravelAnim anim;

        public Builder(Activity context) {
            anim = new BezierTravelAnim();
            anim.setContext(context);
            anim.setAnimContainer((ViewGroup) context.getWindow().getDecorView());
        }

        public Builder setStartPointF(PointF startP) {
            anim.setStartP(startP);
            return this;
        }

        public Builder setEndPointF(PointF endP) {
            anim.setEndP(endP);
            return this;
        }

        public Builder setDuration(int duration) {
            anim.setDuration(duration);
            return this;
        }

        public Builder setAnimView(View animView){
            anim.setAnimView(animView);
            return this;
        }

        public Builder setAnimatorListener(Animator.AnimatorListener animatorListener) {
            anim.animatorListener = animatorListener;
            return this;
        }

        public BezierTravelAnim build() {
            if (anim.getContext() == null) {
                throw new IllegalStateException("context not be null");
            }
            if (anim.getAnimView() == null) {
                throw new IllegalStateException("animView not be null");
            }
            if (anim.getStartP() == null) {
                throw new IllegalStateException("startP not be null");
            }
            if (anim.getEndP() == null) {
                throw new IllegalStateException("endP not be null");
            }
            return anim;
        }
    }

}
